import { Component } from "@angular/core"
import {AlertController,LoadingController,NavParams,NavController} from 'ionic-angular'
import { FileTransfer,FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
import { ImagePicker } from '@ionic-native/image-picker';
import { HttpClient,HttpHeaders } from '@angular/common/http';
import { NativeStorage } from '@ionic-native/native-storage';
import { TranslateService } from '@ngx-translate/core';

import { AppConfig } from '../../../../app/app.config';
@Component({
    selector: 'page-verify',
    templateUrl: 'toVerify.html',
})

export class ToVerifyPage{
    token:string
    loading:any
    src = AppConfig.getMainUrl()
    form={
        certification_type:1,
        certification_url:''
    }
    img1=""
    img2=""
    constructor(
        private imgPicker:ImagePicker,
        private alertCtrl:AlertController,
        private http:HttpClient,
        private stroge:NativeStorage,
        private transfer:FileTransfer,
        private loadingCtrl:LoadingController,
        private navParams:NavParams,
        private navCtl:NavController,
        private translate: TranslateService
    ) {
        this.form.certification_type = this.navParams.get('type')
        this.stroge.getItem('token').then(token=>{
            this.token = token
        })
        translate.setDefaultLang('en')
    }
    ionViewDidEnter(){
        this.stroge.getItem('lang').then(res=>{
            this.translate.use(res)
        })
    }
    alertb(title,subTitle){
        let alert = this.alertCtrl.create({
            title: title,
            subTitle: subTitle,
            buttons: ['OK']
        });
        alert.present();
    }
    loadWait(){
        this.loading = this.loadingCtrl.create({
            content: 'Please wait...'
        });
        this.loading.present();
    }
    imgSelect(index){
        this.imgPicker.getPictures({maximumImagesCount:1}).then((results)=>{
            if(!!results){
                this.loadWait();
                for (var i=0;i<results.length;i++){
                    let fileTransfer: FileTransferObject = this.transfer.create();
                    let options:FileUploadOptions = {
                        fileKey: 'img',
                        chunkedMode: false,
                    }
                    fileTransfer.upload(results[0], AppConfig.getUrl()+'house/upload_img?token='+this.token ,options,true).then((data) => {
                        let response = JSON.parse(data.response)
                        if(response['code']==101){
                            if(index==1){
                                this.img1 = response['data']['thumb_img']
                            }else if(index==2){
                                this.img2 = response['data']['thumb_img']
                            }
                        }else{
                            this.alertb(response['msg'],'');
                        }
                        
                        this.loading.dismiss();
                    }, (err) => {
                        this.alertb('Error!','');
                        this.loading.dismiss();
                    })
                }
            }
        })
    }
    submit(){
        if(this.img1||this.img2){
            this.form.certification_url = this.img1&&this.img2?this.img1+','+this.img2:(this.img1?this.img1:this.img2)
        }else{
            this.alertb('Error!','Please upload at least one!');
        }
        
        this.loadWait();
        this.http.post(AppConfig.getUrl()+'user/authentication?token='+this.token, AppConfig.urlEcode(this.form), { headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' }) }).subscribe( res => {
            this.loading.dismiss()
            if(res['code']===101){
                this.alertb('successed','')
                this.navCtl.pop()
            }else{
                this.alertb('Error!',res['msg']);
            }
        })
    }
}